package com.testpro.easyrest.Util;

public class StringreplaceHelper {

  private static final String placeholderPrefix = "{";
  private static final String placeholderSuffix = "}";

  private final String valueSeparator = "$";

  /**
   * 获取{中的值}
   *
   * @param valueSeparator
   * @return {中的值}
   */
  public static String getKeywords(String valueSeparator) {
    String reg = "(?<=(?<!\\\\)\\{)(.*?)(?=(?<!\\\\)\\})";
    RegExp re = new RegExp();
    return re.findString(reg, valueSeparator);
  }

  /**
   * @param str ${}字符串
   * @return
   */
  private static String getCacheValue(String str) {

    StringBuffer stringBuffer = new StringBuffer();
    if (str.length() > 0) {
      String[] split = str.split("\\$");
      for (String s : split) {
        if (s.contains(placeholderPrefix) && s.contains(placeholderSuffix)) {
          // 获取{}中的引用字符串
          String keywords = getKeywords(s);
          String CacheValue = CacheOrAutoSetVaule(keywords);
          if (CacheValue != null) {
            stringBuffer.append(CacheValue);
          }
        } else {
          stringBuffer.append(s);
        }
      }
    }
    return stringBuffer.toString();
  }

  /**
   * 根据缓存的关键字进行匹配查询 ${request.Cookies.xxx} ${Cookies.xxx} ${xxx}
   *
   * @param keywords
   * @return
   */
  private static String CacheOrAutoSetVaule(String keywords) {
    String result = null;
    String[] split = keywords.split("\\.");
    if (split.length == 1) {
      // 如果是一个 说明就是XXX设计优先读取自动生成数据 如果找不到再找缓存数据
      String StringInfo = split[0];
      result = reautomaticGeneration(keywords);
    }
    if (result != null) {
      return result;
    } else {
      return getCachefromStringData(keywords);
    }
  }

  /**
   * @param keywords
   * @return 精准扶贫模式精准获取数据
   */
  private static String getCacheFromStringPath(String keywords) {

    String[] split = keywords.split("\\.");
    String RequestOrrRsponse = split[0];
    if (RequestOrrRsponse.equals("request")) {
      return SearchdatafromRequestCache(split[1] + "." + split[2]);
    } else if (RequestOrrRsponse.equals("response")) {
      return SearchdatafromResponseCache(split[1] + "." + split[2]);
    } else {
      throw new RuntimeException("getCacheFromStringPath表达式解析失败请按照规则填写！！");
    }
  }

  /**
   * 2个或者3个参数的情况
   *
   * @param keywords
   * @return
   */
  private static String getCachefromStringData(String keywords) {
    String result;
    result = SearchdatafromResponseCache(keywords);
    if (result != null) {
      return result;
    } else {
      result = SearchdatafromRequestCache(keywords);
      if (result != null) {
        return result;
      } else {
        throw new RuntimeException("缓存中找不到对应的值无法进行匹配！！");
      }
    }
  }

  /**
   * 可能存在情况 只有2种 一种是知道key{cookies.auth} 一种是不知道key的{auth}
   *
   * @param stringInfo 从request缓存中找对应的值
   * @return
   */
  private static String SearchdatafromRequestCache(String stringInfo) {

    if (stringInfo.split("\\.").length == 3) {
      return getCacheFromStringPath(stringInfo);
    }

    return null;
  }

  /**
   * 可能存在情况 只有2种 一种是知道key{cookies.auth} 一种是不知道key的{auth}
   *
   * @param stringInfo 从response缓存找对应的值
   * @return
   */
  private static String SearchdatafromResponseCache(String stringInfo) {
    if (stringInfo.split("\\.").length == 3) {
      return getCacheFromStringPath(stringInfo);
    }
    return null;
  }

  /**
   * 根据设定自动生成测试数据
   *
   * @param StringInfo
   * @return
   */
  private static String reautomaticGeneration(String StringInfo) {
    return null;
  }

  public static void main(String[] args) {
    String s = "request.Cookies.xxx";
    String s1 = "request.Cookies";
    String s2 = "Cookies";
    StringreplaceHelper.CacheOrAutoSetVaule(s);
    StringreplaceHelper.CacheOrAutoSetVaule(s1);
    StringreplaceHelper.CacheOrAutoSetVaule(s2);
  }
}
